const path = require('path')
const nodeExternals = require('webpack-node-externals')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const webpack = require('webpack')
const utils = require('./utils')

// const NODE_ENV = (proces.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'prod') ? 'production': "development"
// process.env.NODE_ENV = NODE_ENV

const webpackconfig = {
    target: 'node',
    entry: {
        server: path.join(utils.APP_PATH, 'index.js')
    },
    output: {
        filename: '[name].bundle.js',
        path: utils.DIST_PATH
    },
    module: {
        rules: [ 
            {
                test: /\.(js|jsx)$/,
                use: 'babel-loader',
                exclude: [path.join(__dirname, '/node_modules/')]
            }

        ]
    },
    externals: [nodeExternals()],
    plugins: [
        new CleanWebpackPlugin(),
        new webpack.DefinePlugin({
            "process.env": JSON.stringify(process.env),
            // "process.env": {
            //     // The DefinePlugin replaces variables in your code with other values or expressions at compile time.
            //     NODE_ENV: NODE_ENV
            // }
        })
    ],
    node: {
        console: true,
        global: true,
        process: true,
        Buffer: true,
        __filename: true,
        __dirname: true,
        setImmediate: true,
        path: true
    }
}

// console.log(webpackconfig)
// console.log(process.env)
// debugger

module.exports = webpackconfig